package edu.ucuenca.ec.nosql.IngresoDatos;

import edu.ucuenca.ec.nosql.testing.*;
import java.util.ArrayList;
import org.neo4j.cypher.ExecutionEngine;
import org.neo4j.cypher.ExecutionResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Node;
import org.neo4j.graphdb.Relationship;
import org.neo4j.graphdb.factory.GraphDatabaseFactory;
import org.neo4j.kernel.impl.util.StringLogger;

/**
 *
 * @author fher
 */
public class Consultas {

    private static final String NooDB = "/opt/neo4j-enterprise-2.2.0-M03";
    private static final String Prop = "/opt/neo4j-enterprise-2.2.0-M03/conf/neo4j.properties";
    Node first;
    Node second;
    Relationship relation;
    static GraphDatabaseService graphDatabaseservice;

    public static void main(String[] args) {
        graphDatabaseservice = new GraphDatabaseFactory()
                .newEmbeddedDatabaseBuilder(NooDB)
                .loadPropertiesFromFile(Prop)
                .newGraphDatabase();
        ArrayList<Node> l = buscarAmigos("0");
        for (Node l1 : l) {
            //System.out.println(l1.);
        }
    }

    public static Node buscar(String parametro) {
        ExecutionEngine engine;
        engine = new ExecutionEngine(graphDatabaseservice, StringLogger.SYSTEM);
        String query = "match (n {idUsuario: '" + parametro + "'}) return n";
        ExecutionResult result = engine.execute(query);
        scala.collection.Iterator<Node> it = result.columnAs("n");
        while (it.hasNext()) {
            Node n = it.next();
            System.out.println(n.getProperty("idUsuario"));
            return n;
        }
        return null;
    }

    public static ArrayList<Node> buscarAmigos(String parametro) {
        ExecutionEngine engine;
        engine = new ExecutionEngine(graphDatabaseservice, StringLogger.SYSTEM);
        // String query = "match (n {idUsuario: '" + parametro + "'}) return n";
        String query = "MATCH (n {idUsuario:'" + parametro + "'})-[:ESAMIGODE]->(d) RETURN n";
        ExecutionResult result = engine.execute(query);
        ArrayList<Node> listaAmigos = new ArrayList<>();
        scala.collection.Iterator<Node> it = result.columnAs("n");
        
        while (it.hasNext()) {
            Node next = it.next();
            System.out.println(next.getDegree());
        }
        
        return listaAmigos;
    }
}
